#include "editor.h"
#include "ui_editor.h"

editor::editor(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::editor)
{
    ui->setupUi(this);
    // 设置窗体最大化和最小化
    Qt::WindowFlags windowFlag  = Qt::Dialog;
    windowFlag                  |= Qt::WindowMinimizeButtonHint;
    windowFlag                  |= Qt::WindowMaximizeButtonHint;
    windowFlag                  |= Qt::WindowCloseButtonHint;

    setWindowFlags(windowFlag);
}

editor::~editor()
{
    delete ui;
}

//设置文本内容
void editor::setText(QString s){
    ui->content->setText(s);
    editor::text=s;
}

//设置标题
void editor::setTitle(QString s){
    ui->title->setText(s);
    editor::title=s;
}

//取消
void editor::on_cancel_clicked()
{
//    this->close();
    ui->content->setText(editor::text);
    setReadOnly(true);
}

//保存内容
void editor::on_save_clicked()
{
    QString Rtext=ui->content->toPlainText();
    editor::text=Rtext;
//    退出编辑
    setReadOnly(true);
}

void editor::setRow(int r){
    ui->content->document()->setMaximumBlockCount(r);
}

void editor::setPlace(QString s){
    ui->content->setPlaceholderText(s);
}

//返回保存的文本
QString editor::getText(){
    return editor::text;
}

QString editor::getTitle(){
    return editor::title;
}

void editor::setReadOnly(bool r){
    ui->content->setReadOnly(r);
}

//编辑
void editor::on_edit_clicked()
{
    if(!readOnly)
        setReadOnly(false);
}

void editor::readFile(bool readOnly){
    editor::readOnly = readOnly;
}
